package com.liuqi.aiagent.controller;

import com.liuqi.aiagent.service.AgentService;
import com.liuqi.aiagent.transfer.ApiResult;
import com.liuqi.aiagent.transfer.common.LLMModelParameter;
import com.liuqi.aiagent.transfer.dto.AgentEditDto;
import com.liuqi.aiagent.transfer.dto.AgentPublishUpdateDto;
import com.liuqi.aiagent.transfer.dto.AgentSaveDto;
import com.liuqi.aiagent.transfer.model.Result;
import com.liuqi.aiagent.transfer.model.ResultList;
import com.liuqi.aiagent.transfer.vo.AgentInfoVo;
import com.liuqi.aiagent.transfer.vo.AgentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.liuqi.aiagent.util.StringUtil.notHasText;

/**
 * @author liuqi
 * @date 2025/7/16
 **/
@RestController
@RequestMapping("/agent")
@AllArgsConstructor
@Api(tags = "Agent 服务接口")
public class AgentController {

    private final AgentService agentService;

    @ApiOperation("添加agent")
    @PostMapping("/add")
    public Result add(@RequestBody AgentSaveDto agent) {
        if (StringUtils.isEmpty(agent.getName())) {
            return ApiResult.fail("名称为空");
        }
        if (StringUtils.isEmpty(agent.getDesc())) {
            return ApiResult.fail("描述为空");
        }

        return agentService.save(agent);
    }

    @ApiOperation("编辑agent")
    @PostMapping("/edit")
    public Result edit(@RequestBody AgentEditDto agent) {
        return agentService.edit(agent);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除Agent")
    @ApiImplicitParam(name = "id", value = "Agent Id", required = true, dataTypeClass = Integer.class)
    public Result delete(Integer id) {
        if (StringUtils.isEmpty(id)) {
            return ApiResult.fail("Agent ID为空");
        }
        return agentService.delete(id);
    }

    @GetMapping("/findAll")
    @ApiOperation("查询所有agent")
    public ResultList<AgentVo> findAll() {
        return agentService.findAll();
    }

    @PostMapping("/publish")
    @ApiOperation("发布更新")
    public Result publishUpdate(@RequestBody AgentPublishUpdateDto updateDto) {
        if (updateDto.getAgentId() == null) {
            return ApiResult.fail("Agent ID为空");
        }
        if (updateDto.getModelId() == null) {
            return ApiResult.fail("模型为空");
        }
        if (notHasText(updateDto.getPrompt())) {
            return ApiResult.fail("提示词为空");
        }
        if (updateDto.getLlmModelParameter() != null) {
            LLMModelParameter parameter = updateDto.getLlmModelParameter();
            Float temperature = parameter.getTemperature();
            if (temperature != null) {
                if (temperature < 0 || temperature > 2) {
                    return ApiResult.fail("模型参数: [温度]不合法. 参考值[0 ~ 2]");
                }
            }
            Float topP = parameter.getTopP();
            if (topP != null) {
                if (topP < 0 || topP > 1) {
                    return ApiResult.fail("模型参数: [top_p]不合法. 参考值[0 ~ 1]");
                }
            }
            Float presencePenalty = parameter.getPresencePenalty();
            if (presencePenalty != null) {
                if (presencePenalty < -2 || presencePenalty > 2) {
                    return ApiResult.fail("模型参数: [存在惩罚]不合法. 参考值[-2 ~ 2]");
                }
            }
            Float frequencyPenalty = parameter.getFrequencyPenalty();
            if (frequencyPenalty != null) {
                if (frequencyPenalty < -2 || frequencyPenalty > 2) {
                    return ApiResult.fail("模型参数: [频率惩罚]不合法. 参考值[-2 ~ 2]");
                }
            }
            Integer maxTokens = parameter.getMaxTokens();
            if (maxTokens != null) {
                if (maxTokens < 1024 || maxTokens > 4096) {
                    return ApiResult.fail("模型参数: [maxTokens]不合法. 参考值[1024 ~ 4096]");
                }
            }
            Integer thinkingBudget = parameter.getThinkingBudget();
            if (thinkingBudget != null) {
                if (thinkingBudget < 1024 || thinkingBudget > 4096) {
                    return ApiResult.fail("模型参数: [思考长度]不合法. 参考值[1024 ~ 4096]");
                }
            }
        }

        return agentService.publishUpdate(updateDto);
    }

    @GetMapping("/info")
    @ApiOperation("查询agent详情信息")
    @ApiImplicitParam(name = "agentId", value = "agentId", required = true, dataTypeClass = Integer.class)
    public Result<AgentInfoVo> info(Integer agentId) {
        if (agentId == null) {
            return ApiResult.fail("agentId为空");
        }
        return agentService.info(agentId);
    }

    @GetMapping("/findEventMessages")
    @ApiOperation("查询agent事件消息")
    @ApiImplicitParam(name = "agentId", value = "agentId", required = true, dataTypeClass = Integer.class)
    public ResultList<String> findEventMessages(Integer agentId) {
        if (agentId == null) {
            return ApiResult.failList("agentId为空");
        }
        return agentService.findEventMessages(agentId);
    }

}
